package com.haisen.service.impl;

import com.github.pagehelper.PageHelper;
import com.haisen.core.utils.PageResultSet;
import com.haisen.dataobject.entity.Bill;
import com.haisen.dataobject.entity.Income;
import com.haisen.dataobject.mapper.base.BillMapper;
import com.haisen.dataobject.query.BillQuery;
import com.haisen.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.List;

@Service
public class BillServiceImpl implements BillService {

    @Autowired
    private BillMapper billMapper;

    @Override
    public void create(Bill bill) {
        billMapper.insertSelective(bill);
    }

    @Override
    public void update(Bill bill) {
        billMapper.updateByPrimaryKeySelective(bill);
    }

    @Override
    public void delete(Long id) {
      billMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Bill> findAll() {
        return billMapper.selectAll();
    }

    @Override
    public PageResultSet<Bill> findByPage(BillQuery billQuery) {
        Weekend<Bill> example = Weekend.of(Bill.class);
        WeekendCriteria<Bill,Object> criteria = example.weekendCriteria();
          if (!StringUtils.isEmpty(billQuery.getPaee())){
              criteria.andLike(Bill :: getPaee,"%"+billQuery.getPaee()+"%");
          }
          if (!StringUtils.isEmpty(billQuery.getOrgName())){
            criteria.andLike(Bill :: getOrgName,"%"+billQuery.getOrgName()+"%");
          }
        PageHelper.offsetPage(billQuery.getOffset(), billQuery.getLimit());
         List<Bill> billList = billMapper.selectByExample(example);

        long total = billMapper.selectCountByExample(example);
        PageResultSet<Bill> resultSet = new PageResultSet<>();
        resultSet.setRows(billList);
        resultSet.setTotal(total);
        return resultSet;
    }
}
